window.onload = function() {

	var theGameId = window.name;

	//获取某个游戏数据
	//	$.ajax({
	//		type: 'get',
	//		url: 'http://api.staging.shidaiwl.cn/api/v1/game/member/game/detail/' + theGameId,
	//		json: true,
	//		contentType: 'application/json',
	//		success: function(data) {
	//			if(isNull(data) == '') {
	//				alert('获取游戏详情出错，请稍候再试~~');
	//				return;
	//			}
	//			console.log('获取游戏详情成功 ,data:' + data);
	//			dealGameDetail(data);
	//		},
	//		error: function(data) {
	//			console.log('游戏详情  error,data:' + data);
	//		}
	//	});

	request(base_url + '/game/member/game/detail/' + theGameId)
		.done(function(data) {
			dealGameDetail(data);
		})
		.fail(function() {
			console.log('dealGameDetail error');
		});

	function dealGameDetail(json) {
		//添加第一页的大图
		var div = document.getElementById('parallax');
		var img = document.createElement('img');
		img.src = json.front_cover; //游戏详情里的大图
		div.appendChild(img);

		var detailUl = document.getElementById('detail_ul');
		var newli = document.createElement('li');
		newli.className = 'mui-table-view-cell mui-media main_column';

		/*获取星星显示的span*/
		var starSpan = '';
		var score_avg = json.score_avg; //分数
		var score_avg_x10 = score_avg * 10;
		var score_avg_x10_ = score_avg_x10 % 10;
		if(score_avg_x10_ > 0 && score_avg_x10_ <= 5) {
			needHalfStar = true;
		} else {
			needHalfStar = false;
		}
		//alert(needHalfStar);
		for(var home_hotgame_starnum = 0; home_hotgame_starnum < 5; home_hotgame_starnum++) {
			if(home_hotgame_starnum < parseInt(score_avg)) {
				starSpan += '<span class="iconfont icon-xing1"></span>';
			} else {
				if(home_hotgame_starnum == parseInt(score_avg) && needHalfStar) {
					starSpan += '<span class="iconfont icon-ban_xing2"></span>';
				} else {
					if(home_hotgame_starnum == parseInt(score_avg) && score_avg_x10_ > 5) {
						starSpan += '<span class="iconfont icon-xing1"></span>';
					} else {
						starSpan += '<span class="iconfont icon-xingxing"></span>';
					}
				}
			}
		}

		var gametypeSpan = '';
		var ttj = '{"gametypes":["竞技","休闲"]}';
		var gametypejson = eval('(' + ttj + ')');
		for(var gametypenum = 0; gametypenum < gametypejson.gametypes.length; gametypenum++) {
			var span = document.createElement('span');
			span.className = 'game_type';
			span.innerText = gametypejson.gametypes[gametypenum];
			gametypeSpan += span.outerHTML;
		}

		var tmpHtml = '<img class="mui-media-object mui-pull-left" src="' +
			json.icon + //图片地址
			'"><div class="mui-media-body main_title">' +
			json.game_name + //主标题
			'</div><div class="mui-media-body"><div style="display: inline;">' +
			starSpan + //星星
			'<span class="final_score">&nbsp;&nbsp;' +
			'' + //分数
			'</span></div><span class="mui-pull-right iconfont icon-xiazai hot_download button_download download" data_android_link="' +
			json.android_link + //安卓链接
			'" data_ios_link ="' +
			json.ios_link + //IOS链接
			'" data-code ="' +
			json.code + //data-code
			'">下载</span></div><div class="mui-media-body mui-ellipsis">' +
			gametypeSpan + //类型
			'</div>';

		newli.innerHTML = tmpHtml;
		detailUl.appendChild(newli);

		//一句话评论
		var one_sentence = document.getElementById('comment_content');
		one_sentence.innerHTML = json.tagline + '<div id="comment_tail" style="display:inline;float: right;padding-right: 10px;"><span class="iconfont icon-jichutubiao_jiangli" style="font-size: 0.22rem;line-height: 0.45rem;">热门评论</span></div>';

		//banner图
		var detail_banner = document.getElementById('detail_banner');
		for(var detail_banner_num = 0; detail_banner_num < json.galleries.length; detail_banner_num++) {
			var div = document.createElement('div');
			div.className = 'swiper-slide';
			var img = document.createElement('img');
			img.src = json.galleries[detail_banner_num];
			div.appendChild(img);
			detail_banner.appendChild(div);
		}

		//轮播图点击
		mui('#detail_banner').on('tap', 'div', function() {
			alert(this);
		})

		var mySwiper = new Swiper('.swiper-container', {
			slidesPerView: 2.2,
			slidesOffsetBefore: 15,
			//				spaceBetween: 10,
			direction: 'horizontal', //'horizontal',//'vertical',
			loop: false,
			paginationClickable: true,
			observer: true, //修改swiper自己或子元素时，自动初始化swiper
			observeParents: true, //修改swiper的父元素时，自动初始化swiper
		})

		//描述文字
		var p = document.getElementsByClassName('detail_alltext_content');
		p[0].innerText = json.description;

		var tmp_txt = '开发者：'+json.develop.company + '<br />' + '大小：'+json.apk_size + '<br />' + '下载次数：'+json.download_times + '<br />' + '更新时间：'+ formatTime(json.versions_timestamp) + '<br />' + '版本：'+json.versions;
		
		var div_message = document.getElementsByClassName('detail_alltext_messcontent');
		div_message[0].innerHTML = tmp_txt;
		
		/*设置可以拖动*/
		(function($) {
			var scrollEl = document.getElementById("scroll");
			var scrollApi = $(scrollEl).scroll({
				parallaxElement: '#parallax',
				parallaxRatio: 0.5,
			});
		})(mui);

		var btn_download = document.getElementsByClassName('div_bottom_download_child');
		btn_download[0].setAttribute('data_android_link', json.android_link);
		btn_download[0].setAttribute('data_ios_link', json.ios_link);

		//上面下载小按钮
		var btn_download_small = document.getElementsByClassName('button_download');
		btn_download_small[0].addEventListener('tap', function() {
			download(isIOSClient(), this.getAttribute('data_ios_link'), this.getAttribute('data_android_link'));
		})

		//下载按钮
		mui('.div_bottom_download').on('tap', '.div_bottom_download_child', function() {
			download(isIOSClient(), this.getAttribute('data_ios_link'), this.getAttribute('data_android_link'));
		});

		//切换详情和评论
		mui('#segmentedControl').on('tap', '#btn_comment', function() {
			var div_bottom_download = document.getElementsByClassName('div_bottom_download');
			div_bottom_download[0].style.display = 'none';
			var div_bottom_input = document.getElementsByClassName('div_bottom_input');
			div_bottom_input[0].style.display = 'block';
		});
		mui('#segmentedControl').on('tap', '#btn_describe', function() {
			var div_bottom_download = document.getElementsByClassName('div_bottom_download');
			div_bottom_download[0].style.display = 'block';
			var div_bottom_input = document.getElementsByClassName('div_bottom_input');
			div_bottom_input[0].style.display = 'none';
		});

	}

	//获取某个游戏我的评分
	//	$.ajax({
	//		type: 'get',
	//		url: 'http://api.staging.shidaiwl.cn/api/v1/game/member/score?game_id='+window.name + '&token=' + localStorage.getItem('token'),
	//		/*
	//		 * 参数
	//		?game_id=a0628c4b-825a-48a4-a46c-fabde7fbf2e9*/
	//
	//		json: true,
	//		contentType: 'application/json',
	//		success: function(data) {
	//			if(isNull(data) == '') {
	//				alert('获取某个游戏我的打分出错，请稍候再试~~');
	//				return;
	//			}
	//			console.log('获取某个游戏我的打分成功  data' + data);
	//			getMyScore(data);
	//		},
	//		error: function(data) {
	//			console.log('获取某个游戏我的打分错误 ,data:' + data);
	//		}
	//	});

	//获取某个游戏我的评分
	request(base_url + '/game/member/score', {
			"type": "map",
			"content": {
				"game_id": theGameId,
				"token": localStorage.getItem('token')
			}
		})
		.done(function(data) {
			console.log('获取某个游戏我的打分成功');
			getMyScore(data);
		})
		.fail(function() {
			console.log('获取某个游戏我的打分错误');
		});

	function getMyScore(json) {
		if(json.score == 0) {
			localStorage.setItem('score', '');
			initClickMyStar();
			return;
		}
		localStorage.setItem('score', json.score);
		/*获取星星显示的span*/
		var starSpan = '';
		var score_avg = json.score; //分数
		for(var myscore_starnum = 0; myscore_starnum < 5; myscore_starnum++) {
			if(myscore_starnum < parseInt(score_avg)) {
				starSpan += '<span class="iconfont icon-xing1"></span>';
			} else {
				starSpan += '<span class="iconfont icon-xingxing"></span>';
			}
		}
		document.getElementById('myscore_star').innerHTML = starSpan;
	}

	function initClickMyStar() {
		//我的评分点击
		mui('.icons').on('tap', 'span', function() {
			var index = parseInt(this.getAttribute("data-index"));
			var parent = this.parentNode;
			var children = parent.children;
			if(this.classList.contains("icon-xingxing")) {
				for(var i = 0; i < index; i++) {
					children[i].classList.remove('icon-xingxing');
					children[i].classList.add('icon-xing1');
				}
			} else {
				for(var i = index; i < 5; i++) {
					children[i].classList.add('icon-xingxing')
					children[i].classList.remove('icon-xing1')
				}
			}
			starIndex = index;
			var word = document.getElementById('word');
			switch(starIndex) {
				case 5:
					word.innerText = '力挺';
					sure(5);
					break;
				case 4:
					word.innerText = '不错';
					sure(4);
					break;
				case 3:
					word.innerText = '一般';
					sure(3);
					break;
				case 2:
					word.innerText = '较差';
					sure(2);
					break;
				case 1:
					word.innerText = '糟糕';
					sure(1);
					break;
			}
		});

	}

	function sure(score) {
		var btnArray = ['否', '是'];
		mui.confirm('确定评' + score + '分吗？', '', btnArray, function(e) {
			if(e.index == 1) {
				//确定
				send_score_to_server(theGameId, score);
			} else {
				//取消
				
			}
		})
	}

	//	<!--获取某个游戏评论-->
	//	console.log('http://api.staging.shidaiwl.cn/api/v1/game/member/discuss/' + theGameId + '?token=' + localStorage.getItem('token'));
	//	$.ajax({
	//		type: 'get',
	//		url: 'http://api.staging.shidaiwl.cn/api/v1/game/member/discuss/' + theGameId + '?token=' + localStorage.getItem('token'),
	//		json: true,
	//		contentType: 'application/json',
	//		success: function(data) {
	//			if(isNull(data) == '') {
	//				alert('获取某个游戏评论出错，请稍候再试~~');
	//				return;
	//			}
	//			console.log('获取某个游戏评论成功  data' + data);
	//			dealGameDetailComment(data);
	//		},
	//		error: function(data) {
	//			console.log('获取某个游戏评论失败  error,data:' + data);
	//		}
	//	});

	//获取某个游戏评论
	function requestDetailByGameId(gameid){
		request(base_url + '/game/member/discuss/' + gameid, {
			"type": "map",
			"content": {
				"token": localStorage.getItem('token')
			}
		})
		.done(function(data) {
			console.log('获取某个游戏评论成功 ');
			dealGameDetailComment(data);
		})
		.fail(function() {
			console.log('获取某个游戏评论失败');
		});
	}
	
	requestDetailByGameId(theGameId);

	function dealGameDetailComment(json) {
//		var comment_item_title = document.getElementsByClassName('comment_item_title');
//		comment_item_title[0].innerText = '评论（' + json.count + '）';
		
		var btn_comment = document.getElementById('btn_comment');
		btn_comment.innerText = '评论（' + json.count + '）';
		
		//评论的ul列表
		var comment_ul = document.getElementsByClassName('all_comment_ul');

		for(var comment_num = 0; comment_num < json.count; comment_num++) {
			var li = document.createElement('li');
			li.className = 'mui-table-view-cell mui-media';
			li.id = json.items[comment_num].id;
			li.setAttribute('data-parentId', json.items[comment_num].id);
			li.setAttribute('data-gemeId', json.items[comment_num].game_id);

			/*每个账户星星显示数量*/
			var smallStarSpan = '';
			var score_avg = json.items[comment_num].score; //分数
			for(var myscore_starnum = 0; myscore_starnum < 5; myscore_starnum++) {
				if(myscore_starnum < parseInt(score_avg)) {
					smallStarSpan += '<span class="iconfont icon-xing1"></span>';
				} else {
					smallStarSpan += '<span class="iconfont icon-xingxing"></span>';
				}
			}
			//评论头像部分
			var tmpHtml = '<div class="comment_item"><div class="mui-media-body" style="height: 0.8rem;"><img class="mui-media-object mui-pull-left" src="' +
				json.items[comment_num].from_avatar + //头像
				'"><span class="comment_item_name">用户' +
				json.items[comment_num].from_nickname + //账户名
				'</span><span class="iconfont icon-pl comment_icon mui-pull-right">' +
				json.items[comment_num].children.length + //评论数
				'</span><span class="iconfont icon-zan upvote  mui-pull-right">' +
				json.items[comment_num].praise.count + //点赞数
				'</span><div class="comment_item_star">' +
				smallStarSpan + //星星显示
				'</div></div></div>';

			li.innerHTML = tmpHtml;

			//点过赞的设置颜色
			var ivote = li.getElementsByClassName('upvote');
			if(json.items[comment_num].praise.praised) {
				ivote[0].style.color = '#ff6494';
				//							alert(this.)
			} else {
				ivote[0].style.color = '#333333';
			}

			//评论文字部分
			var comment_text = document.createElement('div');
			comment_text.className = 'comment_text';
			//文字
			var comment_text_content = document.createElement('div');
			comment_text_content.className = 'comment_text_content';
			comment_text_content.innerText = json.items[comment_num].content;
			//日期
			var comment_text_data = document.createElement('div');
			comment_text_data.className = 'comment_text_data';
			comment_text_data.innerText = formatTime(json.items[comment_num].created_timestamp);
			//其他评论
			var comment_others = document.createElement('div');
			comment_others.className = 'comment_others';

			//评论有留言才添加comment_others
			if(json.items[comment_num].children.length > 0) {

				//小三角
				var img_div = document.createElement('div');
				var img = document.createElement('img');
				img.className = 'xiaosanjiao';
				img.src = '/static/components/img/yxxq/sanjiao_128.png';
				img_div.appendChild(img);
				comment_others.appendChild(img_div);

				//回复的ul列表
				var other_ul = document.createElement('ul');
				other_ul.className = 'comment_other_ul';

				for(var childrenNum = 0; childrenNum < json.items[comment_num].children.length; childrenNum++) {
					var other_li = document.createElement('li');

					//child的parent_id和评论的id一样表示直接对评论回复
					if(json.items[comment_num].children[childrenNum].parent_id == json.items[comment_num].id) {

						other_li.setAttribute('data-fromName', json.items[comment_num].children[childrenNum].from_nickname);
						other_li.setAttribute('data-toName', json.items[comment_num].children[childrenNum].to_nickname);
						other_li.setAttribute('data-parentId', json.items[comment_num].children[childrenNum].parent_id);
						other_li.setAttribute('data-id', json.items[comment_num].children[childrenNum].id);
						other_li.setAttribute('data-gemeId', json.items[comment_num].children[childrenNum].game_id);

						other_li.innerText = "用户" + other_li.getAttribute('data-fromName') + ':' + json.items[comment_num].children[childrenNum].content;
					} else {
						other_li.setAttribute('data-fromName', json.items[comment_num].children[childrenNum].from_nickname);
						other_li.setAttribute('data-toName', json.items[comment_num].children[childrenNum].to_nickname);
						other_li.setAttribute('data-parentId', json.items[comment_num].children[childrenNum].parent_id);
						other_li.setAttribute('data-id', json.items[comment_num].children[childrenNum].id);
						other_li.setAttribute('data-gemeId', json.items[comment_num].children[childrenNum].game_id);

						other_li.innerText = other_li.getAttribute('data-fromName') + ' 回复  ' + other_li.getAttribute('data-toName') + ':' + json.items[comment_num].children[childrenNum].content;
					}

					other_ul.appendChild(other_li);
				}
				//把整个其他评论添加进去
				comment_others.appendChild(other_ul);
			}

			//汇总
			comment_text.appendChild(comment_text_content);
			comment_text.appendChild(comment_text_data);
			comment_text.appendChild(comment_others);

			li.appendChild(comment_text);
			comment_ul[0].appendChild(li);
		}

		//设置其他人回复里的监听
		mui('.comment_other_ul').on('tap', 'li', function() {
			var input_area = document.getElementsByClassName('input-text');
			input_area[0].value = ''; //回复其他人时把输入框内容清空
			input_area[0].placeholder = '回复 ' + this.getAttribute('data-fromName');
			input_area[0].setAttribute('data-parentId', this.getAttribute('data-id'));
			input_area[0].setAttribute('data-gemeId', this.getAttribute('data-gemeId'));

			//弹出输入法
			input_area[0].focus();
			return false;
		})

		//点赞按钮
		mui('.mui-media-body').on('click', '.upvote', function() {
			var num = this.innerText;
			if(num == '') {
				num = 0;
			}

			if(this.style.color != 'rgb(255, 100, 148)') {
				this.style.color = '#ff6494';
				this.innerText = parseInt(num) + 1;
				vote_upload(this, true);
			} else {
				this.style.color = '#333333';
				this.innerText = parseInt(num) - 1;
				vote_upload(this, false);
			}

			return false;
		})

		function vote_upload(el, praised) {
			var liObj = el.parentNode;
			while(liObj.nodeName != 'LI' && liObj.parentNode != null) {
				liObj = liObj.parentNode;
			}
			alert(praised?"点赞成功":"取消点赞成功");
			if(praised) {
				send_upvote_to_server(liObj.id);
			} else {
				send_cancelupvote_to_server(liObj.id);
			}
		}

		//评论按钮
		mui('.mui-media-body').on('click', '.comment_icon', function() {
			/*获取前一个不为空的兄弟元素*/
			//					var tempObj = this.previousSibling;
			//					while (tempObj.nodeType != 1 && tempObj.previousSibling != null) {
			//				    	tempObj = tempObj.previousSibling;
			//				    }

			var liObj = this.parentNode;
			while(liObj.nodeName != 'LI' && liObj.parentNode != null) {
				liObj = liObj.parentNode;
			}
			var input_area = document.getElementsByClassName('input-text');
			input_area[0].value = ''; //回复其他人时把输入框内容清空
			input_area[0].placeholder = '回复 ' + liObj.getElementsByClassName('comment_item_name')[0].innerText;
//			alert(liObj.getAttribute('data-parentId'));
//			alert(liObj.getAttribute('data-gemeId'));
			input_area[0].setAttribute('data-parentId', liObj.getAttribute('data-parentId'));
			input_area[0].setAttribute('data-gemeId', liObj.getAttribute('data-gemeId'));

			//弹出输入法
			input_area[0].focus();

			//					alert(tempObj);
			return false;
		})

		//发送按钮
//		var fire = document.getElementById('btn_send_comment');
//		fire.addEventListener('tap', function() {
////			alert('我的打分：' + localStorage.getItem('score'));
//			if(isNull(localStorage.getItem('score')) == '' ) {
//				mui.toast('请先评分后再进行评论');
//				return;
//			}
//
//			var input_area = document.getElementsByClassName('input-text');
//			var content = input_area[0].value;
//			var gemeId = input_area[0].getAttribute('data-gemeId');
//			var parentId = input_area[0].getAttribute('data-parentId');
//			//					alert('fire:'+ 'content--->' + content + '\n' + 'gemeId--->' + gemeId + '\n' + 'parentId--->' + parentId );
////			alert('theGameId:' + theGameId);
//			addMyComment(input_area[0]);
//			return false;
//		})
		
		//fasong
		mui('div').on('tap','.btn_send_comment',function(){
			var input_area = document.getElementsByClassName('input-text');
//			alert(input_area[0].getAttribute('data-parentId'));
//			alert(input_area[0].getAttribute('data-gemeId'));
			
			if(isNull(localStorage.getItem('score')) == '' ) {
				mui.toast('请先评分后再进行评论');
				return;
			}

			
			var content = input_area[0].value;
			var gemeId = input_area[0].getAttribute('data-gemeId');
			var parentId = input_area[0].getAttribute('data-parentId');
			addMyComment(input_area[0]);
			return false;
		})

		//输入框失焦监听
		var input_area = document.getElementsByClassName('input-text');
		input_area[0].addEventListener('blur', function() {
			if(input_area[0].placeholder.indexOf("回复") != -1) {
				input_area[0].placeholder = '我也来说两句';
				input_area[0].setAttribute('data-parentId', '');
				input_area[0].setAttribute('data-gemeId', '');
			}
		})

	}

	function addMyComment(el) {
		//先判断评论内容长度
		if(el.value.length<1){
			mui.toast('还未输入任何字符');
			return;
		}
		
		request(base_url + '/game/member/discuss?token=' + localStorage.getItem('token'), {
			"type": "json",
			"content": {
				"game_id": theGameId,
				"content": el.value,
				"parent_id": el.getAttribute('data-parentId')
			}
		}, "post")
		.done(function(data) {
			console.log('评论成功');
			mui.toast('评论成功');
			
			//重置发送按钮
			mui('div').off('tap','.btn_send_comment')
			
			//评论后清除textarea内容
			el.value = '';
			el.placeholder = '我也来说两句';
			
			//删除原来ul
			var ori_ul = document.getElementsByClassName('all_comment_ul');
			ori_ul[0].innerHTML = "";
			requestDetailByGameId(theGameId);
		})
		.fail(function() {
			console.log('评论失败');
		});
	}

//	function addMyComment(el) {
//		//先判断评论内容长度
//		if(el.value.length<1){
//			mui.toast('评论长度最少为1');
//			return;
//		}
//		
//		var comment_ul = document.getElementsByClassName('all_comment_ul');
//		var li = document.createElement('li');
//		li.className = 'mui-table-view-cell mui-media';
//		/*每个账户星星显示数量*/
//		var mysmallStarSpan = '';
//		var score_avg = localStorage.getItem('score'); //分数
//		for(var myscore_starnum = 0; myscore_starnum < 5; myscore_starnum++) {
//			if(myscore_starnum < parseInt(score_avg)) {
//				mysmallStarSpan += '<span class="iconfont icon-xing1"></span>';
//			} else {
//				mysmallStarSpan += '<span class="iconfont icon-xingxing"></span>';
//			}
//		}
//		//评论头像部分
//		var tmpMyHtml = '<div class="comment_item"><div class="mui-media-body" style="height: 0.8rem;"><img class="mui-media-object mui-pull-left" src="' +
//			localStorage.getItem('avatar') + //头像
//			'"><span class="comment_item_name">用户' +
//			localStorage.getItem('user_name') + //账户名
//			'</span><span class="iconfont icon-pl comment_icon mui-pull-right">' +
//			0 + //评论数
//			'</span><span class="iconfont icon-zan upvote  mui-pull-right">' +
//			0 + //点赞数
//			'</span><div class="comment_item_star">' +
//			mysmallStarSpan + //星星显示
//			'</div></div></div><div class="comment_text"><div class="comment_text_content">' +
//			el.value + //评论内容
//			'</div><div class="comment_text_data">' +
//			formatTime(Date.parse(new Date())/1000) + //评论时间
//			'</div></div>';
//
//		li.innerHTML = tmpMyHtml;
//		comment_ul[0].appendChild(li);
//
//		mui.toast('评论成功');
//		var scroll = document.getElementById('scroll');
//		scroll.scrollTop = scroll.scrollHeight;
//
//		send_reply_to_server(el.value, theGameId, el.getAttribute('data-parentId'));
//		//评论成功后清除textarea内容
//		el.value = '';
//		el.placeholder = '我也来说两句';
//	}
//

};